﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MeizuSDK.Core
{
    /// <summary>
    /// USB消息通知
    /// </summary>
    public static class UsbConnection
    {
        #region DllImport
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern int UsbNotifyApiRegisterUsbNotifyMsg();

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern int UsbNotifyApiGetUsbConnectType();
        #endregion

        /// <summary>
        /// USB连接状态改变后引发次事件
        /// </summary>
        public static event EventHandler<UsbConnectionEventArgs> StatusChanged;

        private static int usbmsg;
        static UsbConnection()
        {
            usbmsg = UsbNotifyApiRegisterUsbNotifyMsg();
            lastStatus = Status;
        }

        private static UsbConnectionStatus lastStatus;
        /// <summary>
        /// 获取当前USB连接状态
        /// </summary>
        public static UsbConnectionStatus Status
        {
            get
            {
                return (UsbConnectionStatus)UsbNotifyApiGetUsbConnectType();
            }
        }
        /// <summary>
        /// 获取USB连接的通知消息
        /// </summary>
        public static int NotifyMessage
        {
            get
            {
                return usbmsg;
            }
        }

        /// <summary>
        /// For internal use only.
        /// </summary>
        public static void OnStatusChanged(uint wParam)
        {
            UsbConnectionStatus status = (UsbConnectionStatus)wParam;

            if (lastStatus != status)
            {
                lastStatus = status;
                if (StatusChanged != null)
                {
                    StatusChanged(null, new UsbConnectionEventArgs(status));
                }
            }
        }
    }
    /// <summary>
    /// USB连接状态
    /// </summary>
    public enum UsbConnectionStatus : int
    {
        MassStorage = 0,
        ActiveSync = 1,
        Detached = 2,
        Error = -1,
    }
    /// <summary>
    /// USB连接事件参数
    /// </summary>
    public class UsbConnectionEventArgs : EventArgs
    {
        public UsbConnectionStatus Status 
        { 
            get; 
            private set; 
        }

        public UsbConnectionEventArgs(UsbConnectionStatus status)
	    {
            Status = status;
	    }
    }
}
